package com.neusmart.tongrentang.activity;

import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.neusmart.tongrentang.R;
import com.neusmart.tongrentang.constants.API;
import com.neusmart.tongrentang.constants.ShortMsgCodeBehavior;
import com.neusmart.tongrentang.dataLoadRequest.DataLoadActivity;
import com.neusmart.tongrentang.model.MParam;
import com.neusmart.tongrentang.result.Result;

/**
 * 忘记密码
 * <p/>
 * roy
 */

public class ForgetPwdActivity extends DataLoadActivity implements View.OnClickListener {

    private EditText etPhone, etCaptcha, etNewPwd, etConfirmPwd;
    private TextView btnGetCaptcha;
    //
    private String phone, captcha, newPwd, confirmPwd;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_forget_pwd;
    }

    @Override
    protected void init() {
        initView();
        setListener();
    }

    private void initView() {
        etPhone = (EditText) findViewById(R.id.forget_pwd_et_phone);
        etCaptcha = (EditText) findViewById(R.id.forget_pwd_et_captcha);
        etNewPwd = (EditText) findViewById(R.id.forget_pwd_et_new_pwd);
        etConfirmPwd = (EditText) findViewById(R.id.forget_pwd_et_confirm_pwd);
        btnGetCaptcha = (TextView) findViewById(R.id.forget_pwd_btn_get_captcha);
    }

    private void setListener() {
        int[] resIds = {R.id.forget_pwd_btn_back, R.id.forget_pwd_btn_get_captcha,
                R.id.forget_pwd_btn_submit};
        for (int id : resIds)
            findViewById(id).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (getCurrentFocus() != null)//隐藏软键盘
            hideSoftInput();
        switch (v.getId()) {
            case R.id.forget_pwd_btn_back:
                onBackPressed();
                break;
            case R.id.forget_pwd_btn_get_captcha:
                checkBeforeGetCaptcha();
                break;
            case R.id.forget_pwd_btn_submit:
                checkBeforeSubmit();
                break;
            default:
                break;
        }
    }

    private void checkBeforeGetCaptcha() {
        phone = etPhone.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            showToast("请输入手机号");
            return;
        }
        loadData(API.GET_SHORT_MSG_CODE, true);
    }

    private void checkBeforeSubmit() {
        phone = etPhone.getText().toString();
        captcha = etCaptcha.getText().toString();
        newPwd = etNewPwd.getText().toString();
        confirmPwd = etConfirmPwd.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            showToast("请输入手机号");
            return;
        } else if (TextUtils.isEmpty(captcha)) {
            showToast("请输入验证码");
            return;
        } else if (TextUtils.isEmpty(newPwd)) {
            showToast("请输入密码");
            return;
        } else if (TextUtils.isEmpty(confirmPwd)) {
            showToast("请输入确认密码");
            return;
        } else if (!newPwd.equals(confirmPwd)) {
            showToast("请确认输入的密码一致");
            return;
        }
        loadData(API.FORGET_PASSWORD, true);
    }

    @Override
    protected void initParams(MParam param) {
        switch (param.getApi()) {
            case GET_SHORT_MSG_CODE:
                param.addParam("phoneNum", phone);
                param.addParam("behavior", ShortMsgCodeBehavior.FORGET_PWD.getValue());
                break;
            case FORGET_PASSWORD:
                param.addParam("phoneNum", phone);
                param.addParam("newPassword", newPwd);
                param.addParam("confirmNewPassword", confirmPwd);
                param.addParam("shortMsgCode", captcha);
                break;
            default:
                break;
        }
    }

    @Override
    protected void disposeResult(API api, String response) {
        if (response == null)
            return;
        switch (api) {
            case GET_SHORT_MSG_CODE:
                Result result = fromJson(response, Result.class);
                showToast(result.getFriendlyMessage());
                if (!result.isSuccess())
                    return;
                startCountDownForNextSend();
                break;
            case FORGET_PASSWORD:
                Result result1 = fromJson(response, Result.class);
                showToast(result1.getFriendlyMessage());
                if (!result1.isSuccess())
                    return;
                finish();
                break;
            default:
                break;
        }
    }

    // 按钮进行倒计时
    protected void startCountDownForNextSend() {
        new TimeCount(60 * 1000, 1000).start();
    }

    class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            btnGetCaptcha.setEnabled(false);
            String waitStr = millisUntilFinished / 1000 + "秒";
            btnGetCaptcha.setText(waitStr + "后重试");
        }

        @Override
        public void onFinish() {
            btnGetCaptcha.setEnabled(true);
            btnGetCaptcha.setText("获取验证码");
        }

    }

}
